home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’93 / DesktopSwitchƒ / UIRoutines.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-18  |  3.4 KB  |  177 lines  |  [TEXT/KAHL]

  1. #include "UIRoutines.h"
  2. #include "DialogRoutines.h"
  3. #include "DesktopSwitch.h"
  4. #include <Packages.h>
  5. #include <Events.h>
  6.  
  7. MenuHandle appleMenu,fileMenu,editMenu;
  8. Boolean timeToQuit = false;
  9.  
  10. void ErrorGripe (short errorID)
  11. {
  12.     SysBeep(1);
  13. }
  14.  
  15.  
  16. void WNEDelay (short ticks)
  17. {
  18.     long startTime;
  19.     EventRecord theEvt;
  20.     
  21.     startTime = TickCount();
  22.     
  23.     do{
  24.         WaitNextEvent (0,&theEvt,ticks,NULL);
  25.     } while (TickCount() - startTime < ticks);
  26. }
  27.  
  28. void BuildMenus ()
  29. {
  30.     appleMenu = GetMenu (kAppleMenu);
  31.     AddResMenu (appleMenu,'DRVR');
  32.     fileMenu = GetMenu (kFileMenu);
  33.     editMenu = GetMenu (kEditMenu);
  34.     InsertMenu (appleMenu,0);
  35.     InsertMenu (fileMenu,0);
  36.     InsertMenu (editMenu,0);
  37.     DrawMenuBar();
  38. }
  39.  
  40. void DoAbout ()
  41. {
  42.     DialogPtr theDlg;
  43.     short theItem;
  44.     
  45.     theDlg = GetNewDialog (130,NULL,(WindowPtr)-1L);
  46.     do {
  47.         ModalDialog ((ProcPtr)StdFilterProc,&theItem);
  48.     } while (theItem != 1);
  49.     DisposeDialog(theDlg);
  50. }
  51.  
  52. void DoMenu (long menuCommand) {
  53.     if (HiWord(menuCommand) == kFileMenu) { //if click in the file menu    
  54.         switch (LoWord(menuCommand)) {
  55.             case kFile_NewSet:
  56.                 CreateDTFileSet();
  57.                 break;
  58.             case kFile_LoadSet:
  59.                 AskUserForDTFile();
  60.                 break;
  61.             case kFile_Prefs:
  62.                 ConfigurePrefs();
  63.                 break;
  64.             case kFile_HideAll:
  65.                 HideItems();
  66.                 break;
  67.             case kFile_Quit:
  68.                 ExitToShell();
  69.                 break;
  70.         }
  71.     }
  72.     if (HiWord(menuCommand) == kAppleMenu) {
  73.         Str31 name;
  74.         
  75.         if (LoWord(menuCommand) == 1)
  76.             DoAbout();
  77.         else {
  78.             GetItem(appleMenu,LoWord(menuCommand),name);
  79.             OpenDeskAcc(name);
  80.         }
  81.             
  82.     }
  83. }
  84.  
  85. void HandleMouse(EventRecord theEvent) {
  86.     short wPart;
  87.     long mResult;
  88.     WindowPtr whichWindow;
  89.     
  90.     //the only click we handle is a menu click
  91.     wPart = FindWindow (theEvent.where,&whichWindow);
  92.     if (wPart == inMenuBar) {
  93.         mResult = MenuSelect (theEvent.where);
  94.         if (HiWord(mResult) != 0) {
  95.             DoMenu(mResult);
  96.             HiliteMenu (0);
  97.         }
  98.     }
  99. }
  100.  
  101. void mel() 
  102. {
  103.     EventRecord theEvent;
  104.     char theChar;
  105.     
  106.     do{
  107.         WaitNextEvent (everyEvent,&theEvent,5,NULL);
  108.         switch (theEvent.what) {
  109.             case mouseDown:
  110.                 HandleMouse(theEvent);
  111.                 break;
  112.             case keyDown:case autoKey:
  113.                 theChar =  (theEvent.message & charCodeMask);
  114.                 if (theEvent.modifiers & cmdKey)
  115.                     DoMenu(MenuKey(theChar));
  116.                 break;
  117.             case kHighLevelEvent:
  118.                 AEProcessAppleEvent(&theEvent);
  119.                 break;
  120.         }
  121.     } while (!timeToQuit);
  122. }
  123.  
  124.  
  125. //based on ThinkRef 2.0 code
  126. pascal    OSErr    MyOpenDocuments (AppleEvent *theAppleEvent,
  127.                     AppleEvent *reply, long handlerRefcon)
  128. {
  129.     FSSpec    myFSS;
  130.     AEDescList    docList;
  131.     OSErr    myErr;
  132.     long    index, itemsInList;
  133.     Size    actualSize;
  134.     AEKeyword    keywd;
  135.     DescType    returnedType;
  136.  
  137.     myErr = AEGetParamDesc(theAppleEvent, keyDirectObject,
  138.                             typeAEList, &docList);
  139.     if (myErr)
  140.         DebugStr("\pError occured  retrieving descriptors.");
  141.     // count the number of descriptor records in the list
  142.     myErr = AECountItems (&docList,&itemsInList);
  143.     for (index=1; index<=itemsInList; index++) {
  144.         myErr = AEGetNthPtr(&docList, index, typeFSS, &keywd,
  145.                             &returnedType, (Ptr)&myFSS,
  146.                             sizeof(myFSS), &actualSize);
  147.         if (myErr)
  148.             DebugStr("\pError occured getting file.");
  149.         LoadDesktopFile(myFSS);
  150.     }
  151.     myErr = AEDisposeDesc(&docList);
  152.     timeToQuit = true;
  153.     return    noErr;
  154. }
  155.  
  156.  
  157. void InitProg() 
  158. {    
  159.     InitGraf (&qd.thePort);
  160.     InitWindows();
  161.     InitFonts ();
  162.     InitDialogs (NULL);
  163.     InitCursor ();
  164.     InitMenus();
  165.     InitAllPacks();
  166.     AEInstallEventHandler (kCoreEventClass,
  167.                             kAEOpenDocuments,
  168.                             (EventHandlerProcPtr)MyOpenDocuments,
  169.                             0,
  170.                             false);
  171.     
  172.     MoreMasters(); MoreMasters(); MoreMasters(); MaxApplZone();
  173.     LoadPrefs();
  174.     BuildMenus();
  175. }
  176.  
  177.